/*
 * Copyright 2009-2010 Nanjing RedOrange ltd (http://www.red-orange.cn)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package redora.client.util;

import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import redora.client.Persistable;

/**
 * Meta data container for attributes of a model.
 * In order to display a field in the GWT client, use this container to get all the
 * necessary info on label and help texts.<br>
 * Redora will generate for each model object a PojoFields class located in the
 * .rdo.gwt.model.fields package. All attributes will be wrapped in this container.
 * <br>
 * For the field labels you must define captions in the object model. One caption for each
 * language you want to support. If a caption is not defined for a table label, the form label
 * will be used. If both table and form are not defined, the attribute's name will be used.
 * <br>
 * Help texts are provided through the help tags in the model. If provided, you could use these
 * for example in the HTML title attribute or add a ?-icon behind the field in the display.
 *
 * @author Redora (www.redora.net)
 */
public class Field {

    public enum Type {
        String, Integer, Long, Double, Boolean, Date, Datetime, Enum, Html, Set, Object
    }

    @NotNull
    public final String name;
    @NotNull
    public final String displayNameTable;
    @NotNull
    public final String displayNameForm;
    @NotNull
    public final String inputID;
    @NotNull
    public final Type type;
    public final boolean notNull;
    @Nullable
    public final String helpInfo;
    public final int maxLength;

    public final Column column;

    /**
     * @param name Original name as in the model. No i18n.
     * @param inputID
     * @param type Equivalent to the attribute label name.
     */
    public Field(@NotNull String name, @NotNull String inputID, @NotNull Type type, Column column) {
        this(name, name, name, inputID, type, false, null, -1, column);
    }
    /**
     * @param name Original name as in the model. No i18n.
     * @param displayNameTable Short display name, used in table view.
     *      Retrieved from the caption tag. If not defined in the caption tag,
     *      this field is the same as displayNameForm.
     * @param displayNameForm Long display name, used in form view. Retrieved
     *      from the caption tag. If not defined in the caption tag, this
     *      field is the same as name.
     * @param inputID
     * @param type Equivalent to the attribute label name.
     * @param notNull True is this field is mandatory.
     * @param helpInfo The help tag, if provided.
     */
    public Field(@NotNull String name, @NotNull String displayNameTable, @NotNull String displayNameForm,
            @NotNull String inputID, @NotNull Type type, boolean notNull, @Nullable String helpInfo, Column column) {
        this(name, displayNameTable, displayNameForm, inputID, type, notNull, helpInfo, -1, column);
    }

    /**
     * @param name Original name as in the model. No i18n.
     * @param displayNameTable Short display name, used in table view.
     *      Retrieved from the caption tag. If not defined in the caption tag,
     *      this field is the same as displayNameForm.
     * @param displayNameForm Long display name, used in form view. Retrieved
     *      from the caption tag. If not defined in the caption tag, this
     *      field is the same as name.
     * @param inputID
     * @param type Equivalent to the attribute label name.
     * @param notNull True is this field is mandatory.
     * @param helpInfo The help tag, if provided.
     * @param maxLength Only for string attributes, the max length properties.
     *      For non-strings this is -1.
     */
    public Field(@NotNull String name, @NotNull String displayNameTable, @NotNull String displayNameForm,
            @NotNull String inputID, @NotNull Type type, boolean notNull, @Nullable String helpInfo,
            int maxLength, Column column) {
        this.name = name;
        this.displayNameTable = displayNameTable;
        this.displayNameForm = displayNameForm;
        this.inputID = inputID;
        this.type = type;
        this.column = column;
        this.notNull = notNull;
        this.helpInfo = helpInfo;
        this.maxLength = maxLength;
    }
}
